package com.sicheng.蓝桥.练习题;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/11 15:37
 */
public class 小朋友排队 {

    //https://www.acwing.com/problem/content/description/1217/

    static int N = 1000010;
    static int n;
    static int[] a = new int[N], d = new int[N], sum = new int[N];

    static int lowbit(int x) {
        return x & -x;
    }

    static void add(int x) {
        for (int i = x; i < N; i += lowbit(i))
            d[i] += 1;
    }

    static int query(int x) {
        int ans = 0;
        for (int i = x; i > 0; i -= lowbit(i))
            ans += d[i];
        return ans;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        for (int i = 1; i <= n; i++) {
            a[i] = scanner.nextInt() + 1;//树状数组从1开始,所以将a[i]+1,防止值等于0
        }

        //树状数组存的是1~a[i]出现的次数
        for (int i = 1; i <= n; i++) {
            add(a[i]);
            sum[i] = query(N - 1) - query(a[i]); //比这个数大的个数
        }

        //该逆序算了，所以维护树状数组不一样，所以算完大的要清0
        Arrays.fill(d, 0);
        for (int i = n; i >= 1; i--) {
            add(a[i]);
            sum[i] += query(a[i] - 1);  //比这个数小
        }

        long res = 0;

        for (int i = 1; i <= n; i++) {
            res += (long) (1 + sum[i]) * sum[i] / 2;
        }

        System.out.println(res);

    }


}
